package com.example.haojiutao.service.impl;

import com.example.haojiutao.common.Result;
import com.example.haojiutao.constant.Enum.ExceptionCodeEnum;
import com.example.haojiutao.mapper.GoodsTypeMapper;
import com.example.haojiutao.model.domain.GoodsType;
import com.example.haojiutao.service.GoodsTypeService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description: 商品种类业务层实现类
 * @Author: ma_lihong
 * @CreateTime： 2023/9/26 14:44
 */
@Slf4j
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Autowired
    private GoodsTypeMapper goodsTypeMapper;

    /*
     * @Description: 添加商品种类
     * @Description: 添加商品种类
     * @Author: ma_lihong
     * @CreateTime: 2024/1/21 14:54
     * @param: goodsType
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result insertType(GoodsType gt) {
        String goodsType = gt.getGoodsType();
        if (!StringUtils.hasLength(goodsType) ) {
            log.warn("商品类型名不能为空，goodsType = {}",goodsType);
            return Result.faile(100001,"商品类型不能为空");
        }
        log.info("添加成功");
        gt.setDelFlag(0);
        gt.setUpdateTime(new Date());
        Integer rows = goodsTypeMapper.insertType(gt);
        if(rows == 1){
            return Result.success("添加成功！");
        }
        return Result.faile("添加失败");
    }
    /*
     * @Description: 删除商品种类
     * @Author: ma_lihong
     * @CreateTime: 2024/1/21 16:29
     * @param: goodsType
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public Result deleteById(Integer typeId) {
        Integer type = goodsTypeMapper.selectCountByParentId(typeId);
        if(type > 0){
            return Result.faile(ExceptionCodeEnum.TYPE_HAS_CHILD);
        }else {
            GoodsType gt = new GoodsType();
            gt.setTypeId(typeId);
            gt.setDelFlag(1);
            gt.setUpdateTime(new Date());
            Integer rows = goodsTypeMapper.deleteType(gt);
            if (rows == 1) {
                return Result.success("删除成功！");
            }
            return Result.faile("删除失败");
        }
    }

    /*
     * @Description: 列表查询
     * @Author: ma_lihong
     * @CreateTime: 2024/1/22 11:06
     * @param: gt
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public List<GoodsType> selectByList(GoodsType gt) {
        PageHelper.startPage(gt.getPageNum(),gt.getPageSize());
        List<GoodsType> goodsType = goodsTypeMapper.selectByList(gt);

        return goodsType;
    }

    /*
     * @Description:根据id查询
     * @Author: ma_lihong
     * @CreateTime: 2024/1/22 15:12
     * @param: goodsType
     * @return: com.example.haojiutao.common.Result
     */
    @Override
    public GoodsType selectById(Integer typeId) {
        GoodsType goodsType = goodsTypeMapper.selectById(typeId);
        log.info("goodsType={}" + goodsType);
        return goodsType;
    }

    @Override
    public GoodsType selectByParentId(Integer parentId) {
        GoodsType goodsType = goodsTypeMapper.selectByParentId(parentId);
        log.info("goodsType={}" + goodsType);
        return goodsType;
    }

    @Override
    public List<GoodsType> selectListByUser(GoodsType gt) {
        PageHelper.startPage(gt.getPageNum(),gt.getPageSize());
        List<GoodsType> goodsType = goodsTypeMapper.selectListByUser(gt);
        return goodsType;
    }

    @Override
    public List<GoodsType> selectlListByParent(GoodsType gt) {
        PageHelper.startPage(gt.getPageNum(),gt.getPageSize());
        List<GoodsType> goodsType = goodsTypeMapper.selectlListByParent(gt);
        return goodsType;
    }
}
